package view.rules;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.swing.*;

import ruletree.rule.Action;
import ruletree.rule.Rule;
import ruletree.rule.Word;

import components.Children;
import components.Component;

import features.Feature;
import features.FeatureList;


import lexicon.LexiconList;

import net.miginfocom.swing.MigLayout;

public class SpelledOutPanelC extends JPanel{
	
	StemPanel stemPanel;
	PrefixPanel prefixPanel;
	InputSuffixPanel inputSuffixPanel;
	SubmitPanel submitPanel;
	FeatureButtonListener featureButtonListener;
	FeaturePane featurePane;
	private JPanel secondPanel;
	private JPanel thirdPanel;
	private JButton featuresButton;
	private JTextArea featuresArea;
	private JScrollPane scrollPane;
	private JLabel commentLabel;
	private JTextArea commentArea;
	
	private LexiconList lexiconList;
	public static String POS;
	
	private String posFeatures;
    private String ruleName;
     
	
	SubmitOkButtonListener submitOkButtonListener;

	
	public SpelledOutPanelC(LexiconList list){
		this.setLayout(new MigLayout());
		this.lexiconList = list;
		//System.out.println(lexiconList.getPOS() + "IM IN SPELLC");
		initComponents();
		addComponents();
	}
	public SpelledOutPanelC(){
		this.setLayout(new MigLayout());
		
		initComponents();
		addComponents();
	}

	private void initComponents() {
		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		
		secondPanel = new JPanel();
		secondPanel.setLayout(new MigLayout());
		stemPanel = new StemPanel();
		prefixPanel = new PrefixPanel();
		
		inputSuffixPanel = new InputSuffixPanel();
		//input
		
		thirdPanel = new JPanel();
		thirdPanel.setLayout(new MigLayout());
		featuresButton = new JButton("Features");

		featuresButton.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		//featureButtonListener.setPOS(lexiconList.getPOS());
        		
        		featureButtonListener.setPOS(posFeatures);
				featurePane.setTextArea(featureButtonListener.getFeatures());
        	}
        });
		
		featurePane = new FeaturePane();
        featurePane.setMinimumSize(new Dimension((int)screenSize.getWidth() - 500, 80));

		featureButtonListener = new FeatureButtonListener();
        featuresButton.addActionListener(featureButtonListener);

		featuresArea = new JTextArea();
		featuresArea.setEditable(false);
		scrollPane = new JScrollPane(featuresArea,  JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);		
		scrollPane.setMinimumSize(new Dimension(1090, 150));
		
		commentLabel = new JLabel("Comment:");
	    commentArea = new JTextArea();
	    commentArea.setMinimumSize(new Dimension((int)screenSize.getWidth() - 200, (int)screenSize.getHeight() - 670));
		
	    submitPanel = new SubmitPanel();
	    submitOkButtonListener = new SubmitOkButtonListener();
        submitPanel.okButton.addActionListener(submitOkButtonListener);
	}

	private void addComponents() {
		
		addToSecondPanel();
		add(secondPanel, "wrap");
		add(inputSuffixPanel, "wrap");
		addToThirdPanel();
		add(thirdPanel, "wrap");
		add(commentLabel, "wrap");
		//add(commentArea, "wrap");
		add(submitPanel);
		
	}
	
	private void addToSecondPanel(){
		secondPanel.add(prefixPanel);
		secondPanel.add(stemPanel);
	}
	
	private void addToThirdPanel(){
		thirdPanel.add(featuresButton);
		thirdPanel.add(scrollPane);
	}
	
	public LexiconList getLexiconList() {
		return lexiconList;
	}
	public void setLexiconList(LexiconList lexiconList) {
		this.lexiconList = lexiconList;
	}
	
	public void setPosFeatures(String posFeatures, String ruleName) {
		this.posFeatures = posFeatures;
		this.ruleName = ruleName;
	}
	
	public void saveRule(){

        Children child = new Children(new ArrayList<Component>());
        FeatureList featureList = featurePane.getFeatureList();
        
        ArrayList<Action> actionList = new ArrayList<>();
        
        
        String actionName = "addMorphophonemic";
        String var = "$1";
        String affix = "";
        
        Rule rule = new Rule(ruleName);

        String TriggerWord = ruleName;
        Component component = Component.createInstance(TriggerWord, true);
        component.setVar(var);
        
        child.addChild(component);
            
         Children actionChildren  = new Children(new ArrayList<Component>());   
         
         // add the features
         
         Component actionComponent = Component.createInstance(TriggerWord, true);  
               
         actionComponent.setVar(var);
         actionChildren.addChild(actionComponent);
         
         Action action = new Action(actionName);
         action.setVar(var);
         action.setFeatureList(featureList);
         action.setChildren(actionChildren);      
            
        actionList.add(action);
         
        rule.setChildrenList(child);
        rule.setActionList(actionList);
        File f = new File("Languages\\Filipino\\XMLRules\\" + ruleName +".xml");
        rule.saveRule(f);
	}
	
	public class SubmitOkButtonListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			if (e.getSource() == submitPanel.okButton)
                saveRule();
			
		}
		
	}

}
